
import axios from "axios";
import utils from "./utils";
import dayjs from "dayjs";
import { getSettings, saveSettings } from "./db";

interface SendResponse {
    status: boolean;
    content: {
        userid: string,
        createTime: string,
        _id: string,
        content: string
    }[];
}

export async function caidan2Job(botid) {
    console.log('caidan2 job ' + dayjs().format('MM-DD HH:mm:ss'))
    const { data: response } = await axios.get<sendResponse>('https://caidan2.com/api/webot/' +botid+ '/send');
    const lastId = await getSettings("", 'caidan2_last_id') || 0;
    if (response.content._id && (response.content._id !== lastId) ){
        await saveSettings("", 'caidan2_last_id', response.content._id);
        await utils.sendMessage(response.content.userid, response.content.content);
    }
}